//
//  EventPhotoView.m
//  Gruphoto
//
//  Created by Thao Nguyen Huy on 1/9/13.
//  Copyright (c) 2013 CNC Software. All rights reserved.
//

#import "EventPhotoView.h"
#import "EventDetailViewController.h"

@implementation EventPhotoView
@synthesize footerView, numberLikesLabel;
@synthesize delegate, selectedImageView,buttonImageThumbnail;

+ (id)viewFromNibWithData:(NSDictionary*)data{
    NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"EventPhotoView" owner:self options:nil];
    EventPhotoView *view = [nibs objectAtIndex:0];
    [view configDisplayWithData:data];
    return view;
}

-(void)configDisplayWithData:(NSDictionary*)data{
    imageInfo = [[NSDictionary alloc] initWithDictionary:data];
    NSLog(@"data = %@",data);
    numberLikesLabel.text = [[data objectForKey:kNumLike] stringValue];
    dispatch_async(dispatch_get_main_queue(), ^{
    footerView.hidden = YES;
    });
}

+(id)viewFromNib{
    EventPhotoView *view = nil;
    NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"EventPhotoView" owner:self options:nil];
    view = [nibs objectAtIndex:0];
    return view;
}

- (IBAction)selectButtonPressed:(id)sender {
    if ([delegate isMemberOfClass:[EventDetailViewController class]]) {
        EventDetailViewController *detailViewController = (EventDetailViewController *)delegate;
        if ((detailViewController.detailMode == EventDetailModeSetting || detailViewController.detailMode == EventDetailModeCreate) && detailViewController.isDeleting) {
            selectedImageView.hidden = !selectedImageView.hidden;
        }
        else {
            [delegate didSelectPhotoAtIndex:self.tag];                    
        }
    }
    else {
        [delegate didSelectPhotoAtIndex:self.tag];
    }
}

- (void)displayImage{
    NSString *link = [NSString stringWithFormat:@"%@%@",kServerUrl,[imageInfo objectForKey:kSource]];
    NSLog(@"link = %@",link);
    // request image
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager downloadWithURL:[NSURL URLWithString:link]
                    delegate:self
                     options:0
                     success:^(UIImage *image, BOOL cached) {
                         NSLog(@"is success download = %@",cached==YES?@"YES":@"NO");
                            dispatch_async(dispatch_get_main_queue(), ^{
                                UIImage *img = [self resizeImage:image];
                                [buttonImageThumbnail setImage:img forState:UIControlStateNormal];
                                [_loading stopAnimating];
                                NSLog(@"frame of button = %f y = %f",buttonImageThumbnail.frame.origin.x,buttonImageThumbnail.frame.origin.y);
                                NSLog(@"size of image height = %f width = %f",img.size.height,img.size.width);
                                NSLog(@"self center = %f %f",self.center.x,self.center.y);
                         });
                     }
                     failure:nil];
}

- (UIImage*)resizeImage:(UIImage*)image{
    CGSize targetSize = buttonImageThumbnail.frame.size;
    return [Global resizeImage:image tosize:targetSize];
/*
        UIImage *sourceImage = image;
        UIImage *newImage = nil;
        CGSize imageSize = sourceImage.size;
        CGFloat width = imageSize.width;
        CGFloat height = imageSize.height;
        CGFloat targetWidth = targetSize.width;
        CGFloat targetHeight = targetSize.height;
        CGFloat scaleFactor = 0.0;
        CGFloat scaledWidth = targetWidth;
        CGFloat scaledHeight = targetHeight;
        CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
        if (CGSizeEqualToSize(imageSize, targetSize) == NO)
        {
            CGFloat widthFactor = targetWidth / width;
            CGFloat heightFactor = targetHeight / height;
            
            if (widthFactor > heightFactor)
            {
                scaleFactor = widthFactor; // scale to fit height
            }
            else
            {
                scaleFactor = heightFactor; // scale to fit width
            }
            
            scaledWidth  = width * scaleFactor;
            scaledHeight = height * scaleFactor;
            
            // center the image
            if (widthFactor > heightFactor)
            {
                thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
            }
            else
            {
                if (widthFactor < heightFactor)
                {
                    thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
                }
            }
        }
        
        UIGraphicsBeginImageContext(targetSize); // this will crop
        
        CGRect thumbnailRect = CGRectZero;
        thumbnailRect.origin = thumbnailPoint;
        thumbnailRect.size.width  = scaledWidth;
        thumbnailRect.size.height = scaledHeight;
        
        [sourceImage drawInRect:thumbnailRect];
        
        newImage = UIGraphicsGetImageFromCurrentImageContext();
        
        if (newImage == nil) {
            NSLog(@"could not scale image");
        }
        //pop the context to get back to the default
        UIGraphicsEndImageContext();
        return newImage;
*/ 
}

- (void)dealloc {
    self.delegate = nil;
    self.footerView = nil;
    self.numberLikesLabel = nil;
    self.selectedImageView = nil;
    [buttonImageThumbnail release];
    [_loading release];
    [super dealloc];
}

- (void)displayFooter {
    if ([numberLikesLabel.text isEqualToString:@"0"] != YES) {
        footerView.hidden = NO;
    }
}

- (void)hideFooter {
    footerView.hidden = YES;
}

@end
